$(function(){
    Date.prototype.format = function(format){ 
        var o = { 
            "M+" : this.getMonth()+1, //month 
            "d+" : this.getDate() //day
        } 

        if(/(y+)/.test(format)) { 
            format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
        } 

        for(var k in o) { 
            if(new RegExp("("+ k +")").test(format)) { 
                format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
            } 
        } 
        return format; 
    }
    var getHashData = function(hash){
        if(!hash) return false;
        var data = {};

        var optArr = hash.split("&");
        var x = 0;
        for(var a in optArr){
            var s = optArr[a].split("=");
            data[s[0]] = s[1];
        }

        return data;
    };

    var tsweek = new Date();
    var tsa=tsweek.format("yyyy-MM-dd");//今天
    tsweek.setDate(tsweek.getDate() - 1);
    var yesterday=tsweek.format("yyyy-MM-dd");//昨天
    tsweek.setDate(tsweek.getDate() - tsweek.getDay() + 1);
    var ts= tsweek.format("yyyy-MM-dd");
    tsweek.setDate(tsweek.getDate()-7);
    var pre= tsweek.format("yyyy-MM-dd");
    tsweek.setDate(tsweek.getDate()+6);
    var prea= tsweek.format("yyyy-MM-dd");
    tsweek.setDate(tsweek.getDate()-5);
    var seven=tsweek.format("yyyy-MM-dd");
    //console.log(a);
    if(location.hash.length>10){
        var be = getHashData(location.hash.substring(1))
        $("#startDate").val(be.btime);
        $("#endDate").val(be.etime);
    }else{
        $("#startDate").val(seven);
        $("#endDate").val(yesterday);
    }
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth()+1;
    if (month<10){
        month = "0"+month;
    }
    var firstDay =year+"-"+month+"-"+"01";
    month=month-1;
    var prfirstDay =year+"-"+month+"-"+"01";
     myDate = new Date(year,month,0);
    var lastDay = year+"-"+month+"-"+myDate.getDate();
    $(".yesterday").click(function(){
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        $("#startDate").val(yesterday);
        $("#endDate").val(yesterday);
        myDate.changeEvent(yesterday, yesterday)
    });//昨天


    $(".tsmonth").click(function(){
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        $("#startDate").val(firstDay);
        $("#endDate").val(tsa);
        myDate.changeEvent(firstDay, tsa)
    });//本月
    $(".prmonth").click(function(){
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        $("#startDate").val(prfirstDay);
        $("#endDate").val(lastDay);
        myDate.changeEvent(prfirstDay, lastDay)
    });//上月

    $(".tswk").click(function(){
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        $("#startDate").val(seven);
        $("#endDate").val(yesterday);
        myDate.changeEvent(seven, yesterday)
    });//本周
    $(".prwk").click(function(){
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        $("#startDate").val(pre);
        $("#endDate").val(prea);
        myDate.changeEvent(pre, prea);
    });//上周
    $(".clear").click(function(){
        $(this).siblings().removeClass("active");
        //$(this).addClass("active");
        $("#startDate").val("");
        $("#endDate").val("");
    });
    $("#startDate,#endDate").click(function(){
        $(".datemsg li").removeClass("active");
    });

    // $("#startDate").on('change', function() {
    //     alert('dadas')
    // })

     $('td').on('click',function(e){
        console.log($('#startDate').val());
    });

 }); 